#pragma once


/*
 * Input management using system functionality
 * WARNING: These functions are not cross-platform
 * On Linux, sending fake input requires root access
 * NOTE: ncurses should be used on Linux instead of get_key()
 * Dependencies: bzbutil
 */


#include "keys.h"


namespace bzbinput {


#if defined(WINDOWS)


/*
 * Waits until a key is pressed and returns it
 */

Key get_key(const unsigned delta = 66);


/*
 * Emulates a keyboard by sending fake input data
 */

void fake_put(const Key key);


#elif defined(LINUX)


/*
 * Type of key event
 */

enum class KeyPressType : short {
	KEY_RELEASE = 0,
	KEY_PRESS = 1
};


/*
 * Checks if input from stdin is available
 */

bool is_input_available();


/*
 * Waits until a key is pressed and returns it
 * WARNING: This function doesn't return control keys
 * like KEY_CONTROL_LEFT and KEY_SHIFT_LEFT
 */

Key get_key(const unsigned delta = 66);


/*
 * Emulates a keyboard by sending fake input data
 */

bool fake_put(const int devfd, const Key key, const KeyPressType type);


/*
 * Creates a new virtual keyboard device and waits <wait_time> milliseconds
 */

int new_virtual_kb_device(const char* name,
						  const unsigned int wait_time = 500,
						  const unsigned short bus_type = 0x03, // USB
						  const unsigned short vendor = 0x01,
						  const unsigned short product = 0x02,
						  const unsigned short version = 0x03);


/*
 * Destroys the specified virtual keyboard device
 */

bool destroy_virtual_kb_device(const int devfd);


/*
 * Emulates a keyboard by sending fake input data
 */

inline void fake_put(const int devfd, const Key key) {

	fake_put(devfd, key, KeyPressType::KEY_PRESS);
	fake_put(devfd, key, KeyPressType::KEY_RELEASE);
}


#endif

}
